//
//  XCSingerDetailController.m
//  TTLestening
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCSingerDetailController.h"
#import "NetworkingHandlerOfBlock.h"
#import "XCCellForSIngerSection.h"
#import "XCModelForSectionSinger.h"
#import "NetworkingHandlerOfBlock.h"
#import "XCSingerHeaderView.h"
#import "XCModelForSingerHeader.h"
#import "XCAVMusicPlayer.h"
#import "XCCellForSinger.h"

#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667

#define WIDGHT  self.view.frame.size.width
#define HEIGHT  self.view.frame.size.height


@interface XCSingerDetailController ()<UITableViewDataSource, UITableViewDelegate>

//@property (nonatomic, retain)UITableView *tableview;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, assign)BOOL hiden;
@property (nonatomic, assign)NSInteger index;

@end

@implementation XCSingerDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    [self handelData];
    [self createTableview];
    
    self.hiden = YES;

    // 通知中心的隐藏分区下的 cell 的操作
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"apperCell" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note)
    {
       
        
        // 点击 button 添加通知, 是否让分区下面的 cell隐藏
        
        
        if (self.hiden == YES) {
            
            self.hiden = NO;
            self.index = [[note.userInfo objectForKey:@"index"] intValue];
            
            [self.tableview reloadData];
            
            
        }else {
            
            self.hiden = YES;
            
            [self.tableview reloadData];
            
        }
        
    }];
    
    
}

- (void)handelData {

   // NSLog(@"%@", self.url);
    
    // tableViewHeader
    
    [NetworkingHandler networkingWithUrl:self.url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        XCModelForSingerHeader *model = [[XCModelForSingerHeader alloc] init];
        NSDictionary *dic = [result objectForKey:@"data"];
        [model setValuesForKeysWithDictionary:dic];

        XCSingerHeaderView *view = [[XCSingerHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDGHT, HEIGHT / 3) model:model];
        self.tableview.tableHeaderView = view;

    }];
    
    
    
    //区头的数据处理
   // NSLog(@"%@", self.urlForCell);
    [NetworkingHandler networkingWithUrl:self.urlForCell completeHandle:^(NSURLResponse *response, NSData *data, id result) {
       
        self.array = [NSMutableArray array];
        
        NSArray *array = [result objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            
            XCModelForSectionSinger *model = [[XCModelForSectionSinger alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.array addObject:model];
            
        }
        
        [self.tableview reloadData];
        
    }];
    


}


- (void) createTableview {

    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0,0, WIDGHT, HEIGHT - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.tableview registerClass:[XCCellForSIngerSection class] forHeaderFooterViewReuseIdentifier:@"cellForHeader"];
    
    [self.tableview registerClass:[XCCellForSinger class] forCellReuseIdentifier:@"cellForSinger"];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  
    XCCellForSIngerSection *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cellForHeader"];
    
    XCModelForSectionSinger *model = [self.array objectAtIndex:section];
    
    [cell passModel:model sectionNumber:section + 1];
    
    [cell.button addTarget:self action:@selector(hidenAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.backButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
 
    cell.backButton.tag = section;
    cell.button.tag = section;
    
    
    return cell;

}

/**区头的点击事件 */
- (void)playAction:(UIButton *) button {

    XCAVMusicPlayer *music = [XCAVMusicPlayer sharedMusicPlayer];
    
   // XCModelForSectionSinger *model = [self.array objectAtIndex:button.tag];
    
   // [music passMusicUrl:[[model.urlList firstObject] objectForKey:@"url"]];
    [music passMusicUrl:self.array index:button.tag];
    
     [[NSNotificationCenter defaultCenter] postNotificationName:@"giveValue" object:nil userInfo:@{@"modelArray": self.array, @"index": [NSString stringWithFormat:@"%ld",button.tag]}];
    
}

/** 隐藏分区下相应的 cell 事件*/
- (void)hidenAction:(UIButton *) button {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"apperCell" object:nil userInfo:@{@"index": [NSString stringWithFormat:@"%ld",(long)button.tag]}];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return self.view.frame.size.height / 8;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.array.count;

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   XCCellForSinger *cell = [tableView dequeueReusableCellWithIdentifier:@"cellForSinger"];
   
   // cell.backgroundColor = [UIColor redColor];
    
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    
    if (self.hiden == NO && indexPath.section == self.index) {
        
        
            return 80;

        
    }else {
        
        
        return 0;
        
    }

    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
